package com.laikan.reader.article.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.laikan.library.utils.DeviceUtil;
import com.laikan.reader.R;
import com.laikan.reader.core.LaikanApplication;
import com.laikan.reader.core.bean.ArticleSearchBean;

import java.util.ArrayList;

import static com.laikan.reader.R.id.iv_bookfocus_item_icon;

/**
 * Created by Administrator on 2016/11/8.
 */
public class ArticleSearchAdapter extends BaseAdapter {

    public static final int TYPE_CONTENT = 1; //  内容与图片类型
    public static final int TYPE_TEXT = 0; // 文本类型
    public static final int TYPE_IMG = 2; //图片类型

    private Context mContext;
    private LayoutInflater layoutInflater;
    private ArrayList<ArticleSearchBean.BookFocusDataList> articleDataList;

    public ArticleSearchAdapter(Context context, ArrayList<ArticleSearchBean.BookFocusDataList> datas) {
        this.mContext = context;
        this.articleDataList = datas;
        layoutInflater = LayoutInflater.from(mContext);
    }

    //添加数据
    public void addDatas(ArrayList<ArticleSearchBean.BookFocusDataList> datas) {
        this.articleDataList.addAll(datas);
        notifyDataSetChanged();
    }

    //获取数据
    public ArrayList<ArticleSearchBean.BookFocusDataList> getDatas() {
        return this.articleDataList;
    }

    //更新数据
    public void updateDatas(ArrayList<ArticleSearchBean.BookFocusDataList> datas) {
        this.articleDataList = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return articleDataList.size();
    }

    @Override
    public Object getItem(int i) {
        return articleDataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemViewType(int position) {
        if (Integer.parseInt(articleDataList.get(position).getType()) == 1) {
            return TYPE_TEXT;
        }else if (Integer.parseInt(articleDataList.get(position).getType()) == 2) {
            return TYPE_CONTENT;
        }
        return TYPE_IMG;
    }

    public void clearData() {
        articleDataList.clear();
        this.notifyDataSetChanged();
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        ViewHolderContent viewHolderContent = null;
        ViewHolderText viewHolderText = null;
        ViewHolderImg viewHolderImg = null;
        ArticleSearchBean.BookFocusDataList articleBean = null;
        int type = 0;
        if (position <= articleDataList.size() - 1) {
            type = getItemViewType(position);
            articleBean = articleDataList.get(position);
        }
        if (convertView == null) {
            switch (type) {
                case TYPE_CONTENT:
                    convertView = layoutInflater.inflate(R.layout.bookfocus_content_item, null);
                    viewHolderContent = new ViewHolderContent();
                    viewHolderContent.tv_title = (TextView)convertView.findViewById(R.id.tv_title);
                    viewHolderContent.tv_author = (TextView)convertView.findViewById(R.id.tv_author_name);
                    viewHolderContent.tv_read_total = (TextView)convertView.findViewById(R.id.tv_read_total);
                    viewHolderContent.article_img = (SimpleDraweeView)convertView.findViewById(iv_bookfocus_item_icon);
                    convertView.setTag(viewHolderContent);
                    break;
                case TYPE_TEXT:
                    convertView = layoutInflater.inflate(R.layout.bookfocus_text_item, null);
                    viewHolderText = new ViewHolderText();
                    viewHolderText.tv_title = (TextView)convertView.findViewById(R.id.tv_bookfocus_item_title);
                    viewHolderText.tv_content = (TextView)convertView.findViewById(R.id.tv_content);
                    viewHolderText.tv_author = (TextView)convertView.findViewById(R.id.tv_author_name);
                    viewHolderText.tv_read_total = (TextView)convertView.findViewById(R.id.tv_read_total);
                    convertView.setTag(viewHolderText);
                    break;
                case TYPE_IMG:
                    convertView = layoutInflater.inflate(R.layout.bookfocus_img_item, null);
                    viewHolderImg = new ViewHolderImg();
                    viewHolderImg.tv_title = (TextView)convertView.findViewById(R.id.tv_bookfocus_item_title);
                    viewHolderImg.tv_author = (TextView)convertView.findViewById(R.id.tv_author_name);
                    viewHolderImg.tv_read_total = (TextView)convertView.findViewById(R.id.tv_read_total);
                    viewHolderImg.article_img = (SimpleDraweeView)convertView.findViewById(iv_bookfocus_item_icon);
                    convertView.setTag(viewHolderImg);
                    break;
                default:
                    break;
            }
        }else {
            switch (type) {
                case TYPE_CONTENT:
                    viewHolderContent = (ViewHolderContent)convertView.getTag();
                    break;
                case TYPE_TEXT:
                    viewHolderText = (ViewHolderText)convertView.getTag();
                    break;
                case TYPE_IMG:
                    viewHolderImg = (ViewHolderImg)convertView.getTag();
                    break;
                default:
                    break;
            }
        }
        switch (type) {
            case TYPE_CONTENT:
                viewHolderContent.tv_title.setText(articleBean.getTitle());
                viewHolderContent.tv_author.setText(articleBean.getAuthor());
                viewHolderContent.tv_read_total.setText(DeviceUtil.getHtmlFormatedString(LaikanApplication.getInstance(), R.string.bookfocus_article_record, articleBean.getReadNum()));
                if (!TextUtils.isEmpty(articleBean.getImg())) {
                    viewHolderContent.article_img.setImageURI(Uri.parse(articleBean.getImg()));
                }
                break;
            case TYPE_TEXT:
                viewHolderText.tv_title.setText(articleBean.getTitle());
                viewHolderText.tv_content.setText(articleBean.getIntro());
                viewHolderText.tv_author.setText(articleBean.getAuthor());
                viewHolderText.tv_read_total.setText(DeviceUtil.getHtmlFormatedString(LaikanApplication.getInstance(), R.string.bookfocus_article_record, articleBean.getReadNum()));
                break;
            case TYPE_IMG:
                viewHolderImg.tv_title.setText(articleBean.getTitle());
                viewHolderImg.tv_author.setText(articleBean.getAuthor());
                viewHolderImg.tv_read_total.setText(DeviceUtil.getHtmlFormatedString(LaikanApplication.getInstance(), R.string.bookfocus_article_record, articleBean.getReadNum()));
                if (!TextUtils.isEmpty(articleBean.getImg())) {
                    viewHolderImg.article_img.setImageURI(Uri.parse(articleBean.getImg()));
                }
                break;
        }
        return convertView;
    }

    class ViewHolderText {
        TextView tv_title, tv_content, tv_author, tv_read_total;
    }

    class ViewHolderContent {
        TextView tv_title, tv_author, tv_read_total;
        SimpleDraweeView article_img;
    }

    class ViewHolderImg {
        TextView tv_title, tv_author, tv_read_total;
        SimpleDraweeView article_img;
    }
}
